
class P10015 {

	static final int MAX = 50000;
	
	private static boolean[] primes = new boolean[MAX];
	private static int[] primos = new int[MAX];

	public static void main(String[] args) {
		createPrimes();
		String line = readLn();
		while (!"0".equals(line)) {
			process(Integer.parseInt(line));
			line = readLn();
		}
	}

	private static void process(int i) {
		boolean[] vivos = new boolean[i];
		for (int k = 0; k < i; k++) {
			vivos[k] = true;
		}
		int mortos = 0;
		int index = -1;

		int primosCounter = 0;
		while(mortos < i - 1) {
			int nextPrimo = primos[primosCounter];
			while (nextPrimo > 0) {
				index++;
				if (index == i) {
					index = 0;
				}

				if (vivos[index]) {
					nextPrimo--;
				}
			}
			vivos[index] = false;
//			System.out.println("Morreu! " + index);
			mortos++;
			primosCounter++;
		}
		
		for (int k = 0; k < i; k++) {
			if (vivos[k]) {
				System.out.println((k+1));
				return;
			}
		}
//		System.out.println(".,.,.,.,.");
	}

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}

	static void createPrimes() {
		   int i, j;

		   for(i = 0; i < MAX; i++) {
		      primes[i] = false;
		   }

		   primes[2] = true;
		   primes[3] = true;
		   primos[0] = 2;
		   primos[1] = 3;
		   
		   int counter = 2;

		   for(i = 5; i < MAX; i+=2) {
		       for(j = 3; j*j <=i ; j+=2)
		           if(i % j == 0) break;
		       if(i % j != 0 )  {
		    	   primes[i] = true;
		    	   primos[counter] = i;
		    	   counter++;
		       }
		   }
		   
		}
	
}
